<?php
class main extends CI_Controller {

	function Main(){
		parent::__construct();
		
		$this->load->model('m_main', '', TRUE);
		$this->m_public_function->clear_cache();
		// if(!$_SESSION[SESSION_USERID])
			// redirect('c=c_login', 'refresh');
			checkLogin();
	}
	
	function index(){
		$locked=TRUE;
		$version="FULL";
		if($this->m_main->check_license($version)){
			$locked=FALSE;
		}
		
		$data_setting_menus = array();
		$data_list_menus = array();
		$data_menus_parent = array();
		$data_menus_child = array();
		
		if($locked){
			$data_list_menus = $this->m_main->get_menus("locked");
			$data["menus"] = $data_list_menus;
			$data_menus_parent = $this->m_main->get_menus_parent("locked");
			$data_setting_menus = $this->m_main->get_setting_menus("locked");
			//$data["submenus"]=$this->m_main->get_shortcuts("locked");
		}else{
			$data_list_menus = $this->m_main->get_menus("open");
			$data["menus"] = $data_list_menus;
			$data_menus_parent = $this->m_main->get_menus_parent("open");
			$data_setting_menus = $this->m_main->get_setting_menus("open");
			//$data["submenus"]=$this->m_main->get_shortcuts("open");
		}
		
		//generate setting-menus
		$generate_setting_menus = "[";
		//foreach
		$i = 0;
		foreach ($data_setting_menus as $setting_menus){
			if($i>0){
				$generate_setting_menus .= ",";
			}
$generate_setting_menus .= "{
	id: '".$setting_menus['menu_title']."',
	text: '".$setting_menus['menu_title']."',
	href: 'index.php".$setting_menus['menu_link']."',
	iconCls: 'icon-".$setting_menus['menu_id']."',
	leaf: true
}";
			$i++;
		}
		$generate_setting_menus .= "]";
		
		write_file('./assets/setting_menus.json', $generate_setting_menus);
		
		//generate list-menus
		$generate_list_menus = "[";
		//foreach
		$i = 0;
		foreach ($data_menus_parent as $menus_parent){
			if($i>0){
				$generate_list_menus .= ",";
			}
			$generate_list_menus .= "{";
			
			$data_menus_child = $this->m_main->get_menus_child($menus_parent['menu_id']);
			if(sizeof($data_menus_child) > 0){
				$generate_list_menus .= "text: '".$menus_parent['menu_title']."',iconCls: 'icon-".$menus_parent['menu_id']."',singleClickExpand: true,children: [";
				$c = 0;
				foreach ($data_menus_child as $menus_child){
					if($c>0){
						$generate_list_menus .= ",";
					}
$generate_list_menus .= "{
	id: '".$menus_child['menu_title']."',
	text: '".$menus_child['menu_title']."',
	href: 'index.php".$menus_child['menu_link']."',
	iconCls: 'icon-".$menus_child['menu_id']."',
	leaf: true
}";					
					$c++;
				}
				$generate_list_menus .= "]";
			}else{
$generate_list_menus .= "id: '".$menus_parent['menu_title']."',
	text: '".$menus_parent['menu_title']."',
	href: 'index.php".$menus_parent['menu_link']."',
	iconCls: 'icon-".$menus_parent['menu_id']."',
	leaf: true";
			}
			
			$generate_list_menus .= "}";
			
			$i++;
		}
		$generate_list_menus .= "]";
		
		write_file('./assets/list_menus.json', $generate_list_menus);
		
		
		$data["background"]=$this->m_main->get_background();
		$this->load->vars($data);
		$this->load->view('v_dekstop');
	}
	
	function welcome(){
		$this->load->view('welcome');
	}
	
	function logout(){
		setLogout();
	/*
		$sesslog=array(
				'SESSION_PERIODE'=>'',
				'SESSION_PERIODE_AWAL'=>'',
				'SESSION_KURS_ID'=>'',
				'SESSION_KURS_NAMA'=>'',
				'SESSION_KURS_NILAI'=>'',
				'SESSION_CABANG'=>'',
				'SESSION_CABANG_KODE'=>'',
				'SESSION_CABANG_NAMA'=>'',
				'SESSION_CABANG_COMPANY'=>'',
				'SESSION_CABANG_COMPANY_NAME'=>'',
				'SESSION_JBELI'=>'',
				'SESSION_JJUAL'=>'',
				'SESSION_JKASBANK'=>''
		);
			
		$this->session->unset_userdata($sesslog);
	
		$sess_users=array(
				'SESSION_USERID'=>'',
				'SESSION_GROUPID'=>'',
				'SESSION_GROUPNAMA'=>''
		);
		$this->session->unset_userdata($sess_users);
			
		$this->session->set_flashdata('error',"You've been logged out!");
		$this->session->sess_destroy();
		redirect('','location',302);
		*/
	}
	

}
?>
